<template>
    <div class="train-classes">
        <!--        添加/修改角色-->
        <el-dialog
                class="dialog-center"
                :title="addForm.id?'修改班级':'新增班级'"
                :visible.sync="addForm.visible"
                :close-on-click-modal="false"
                width="30%">
            <el-form :model="addForm" :rules="rules" ref="addForm" label-width="150px">
                <el-form-item label="班级名称" prop="className" style="text-align:left">
                    <el-input v-model="addForm.className" size="mini" style="width:70%;"
                              placeholder="班级名称"></el-input>
                </el-form-item>

            </el-form>
            <div align="center">
                <el-button type="primary" @click="submitForm('addForm')" size="small">{{addForm.id?'提交修改':'立即创建'}}
                </el-button>
                <el-button @click="resetForm('addForm')" size="small">重置</el-button>
            </div>

        </el-dialog>
        <el-row type="flex" justify="start">
            <el-col :span="24" style="text-align:left">
                <el-form :inline="true" :model="searchParam" class="demo-form-inline">
                    <el-form-item label="班级名称">
                        <el-input v-model="searchParam.className" placeholder="班级名称" size="mini"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="el-icon-search" size="mini" @click="searchClassesList()">查询
                        </el-button>
                        <el-button type="primary" icon="el-icon-plus" size="mini" @click="showAddClassesDialog()">添加班级
                        </el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="24">
                <el-table
                        ref="multipleTable"
                        :data="tableData"
                        tooltip-effect="dark"
                        highlight-current-row
                        style="width: 100%">
                    <el-table-column
                            prop="className"
                            label="名称"
                            align="center"
                            show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column
                            label="状态"
                            align="center"
                            show-overflow-tooltip>
                        <template slot-scope="scope">
                            <span :class="scope.row.enable?'c-0f65fa':'c-e09017'">{{scope.row.enable?'正常':'已禁用'}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" min-width="150" fixed="right" align="center">
                        <template slot-scope="scope">
                            <el-button size="mini" :type="scope.row.enable?'warning':'info'"
                                       @click="forbidden(scope.row)">
                                {{scope.row.enable?'禁用':'解禁'}}
                            </el-button>
                            <el-button size="mini" type="primary" @click="modifyclass(scope.row)">修改
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-col>
        </el-row>
    </div>
</template>

<script>
    import {getClassListByOrg, addOneClasses, updateOneClasses} from "@/api/classesApi";
    import CommonUtils from "@/utils/CommonUtils";

    export default {
        name: "trainClasses",
        data() {
            return {
                tableData: [],
                searchParam: {
                    className: null
                },
                addForm: {
                    visible: false,
                    id: null,
                    className: null,
                    enable: true
                },
                rules: {
                    className: [{required: true, message: '请输入班级名称', trigger: 'blur'}],
                }
            }
        },
        created() {
            this.searchClassesList();
        },
        methods: {
            //查询用户机构下的班级
            searchClassesList() {
                getClassListByOrg(this.searchParam).then(res => {
                    this.tableData = res;
                })
            },
            showAddClassesDialog() {
                this.addForm.visible = true;
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            //显示修改dialog
            modifyclass(row) {
                this.addForm = CommonUtils.setValueFromOtherObjectByKey(this.addForm, row);
                this.addForm.visible = true;
            },

            //删除
            forbidden(row) {
                this.$confirm(`是否确定${row.enable ? '禁用' : '解除禁用'}？`, '重要提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.addForm = CommonUtils.setValueFromOtherObjectByKey(this.addForm, row);
                    this.addForm.enable = !row.enable;
                    this.updateOneClasses();
                }).catch(() => {
                })
            },

            //新增/修改
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.addForm.id ? this.updateOneClasses() : this.addOneClasses();

                    } else {
                        this.$message.warning("请将信息填写完整");
                    }
                });
            },
            //修改
            updateOneClasses() {
                updateOneClasses(this.addForm).then(res => {
                    this.addForm.visible = false;
                    this.$message.success('修改成功');
                    this.searchClassesList();
                })
            },
            //新增
            addOneClasses() {
                addOneClasses(this.addForm).then(res => {
                    this.addForm.visible = false;
                    this.$message.success('添加班级成功');
                    this.searchClassesList();

                })
            },
        }
    }
</script>

<style scoped>
    .train-classes {
    }

</style>